(function (angular) {
  var module = angular.module("userApp", ["tm.pagination", "userFilter"]);
  module.controller("userCtrl", ["$scope", "$http", function ($scope, $http) {
    // 初始化数据
    $scope.init = function () {
      $scope.id = "";
      $scope.name = "";
      $scope.account = "";
      $scope.citID = "";
      $scope.sex = "1";
      $scope.role = "1";
      $scope.email = "";
      $scope.mobile = "";
      $scope.useMark = "1";
      $scope.dep = "";
    };
    $scope.init();
    // 查询参数
    $scope.params={
      name:"",
      mobile:""
    };
    // 分页参数:
    $scope.page = {
      currentPage: 1,
      totalItems: 0,
      itemsPerPage: 5,
      pagesLength: 5,
      onChange: function () {
        $scope.getUserList($scope.page.currentPage, $scope.page.itemsPerPage)
      }
    };
    $scope.url = "http://127.0.0.1:8080/lx/";
    // 获取用户列表
    $scope.getUserList = function (currPage, size) {
      if (!currPage && currPage != 0) {
        currPage = $scope.page.currentPage;
      }
      currPage -= 1;
      if (!size) {
        size = $scope.page.itemsPerPage;
      }
      var params = {
        page: currPage,
        size: size ? size : $scope.page.itemsPerPage,
        sort: ' addDateTime desc'
      };
      angular.extend(params,$scope.params);
      layer.load();
      $http({
        method: "post",
        url: $scope.url + "getUserListPages",
        data: params,
      }).success(function (res) {
        layer.closeAll("loading");
        console.log(res);
        if (res.code === 0) {
          $scope.page.content = res.data.content;
          $scope.page.totalItems = res.data.totalElements;
          $scope.page.currentPage = currPage + 1;
          $scope.page.itemsPerPage = size;
        }
      })
    };
   // 搜索查询
    $scope.search=function(){
      $scope.getUserList($scope.page.currentPage, $scope.page.itemsPerPage)
    };
    // 删除
    $scope.delete = function (id) {
      var cf = confirm("是否删除?");
      if (cf) {
        layer.load();
        $http({
          method: "post",
          url: $scope.url + "deleteUser",
          data: id
        }).success(function (res) {
          layer.closeAll("loading");
          if (res.code===0) {
            layer.alert("删除成功!");
            $scope.getUserList($scope.page.currentPage, $scope.page.itemsPerPage)
          } else {
            layer.alert("删除失败!");
          }
        })
      }
    };
    // 编辑
    $scope.edit = function (id) {
      $scope.getDepList();
      $scope.init();
      $(".user").fadeIn();
      if (id) {
        $scope.headName = "编辑用户";
        layer.load();
        $http({
          method: "post",
          url: $scope.url + "getUserById",
          data: id,
        }).success(function (res) {
          layer.closeAll("loading");
          if (res.code === 0) {
            // console.log(res);
            $scope.id = res.data.id;
            $scope.name = res.data.perName;
            $scope.account = res.data.account;
            $scope.citID = res.data.citID;
            $scope.sex = res.data.sexCode;
            $scope.role = res.data.role;
            $scope.email = res.data.email;
            $scope.mobile = res.data.mobile;
            $scope.useMark = res.data.useMark;
            $scope.dep = res.data.department.id;
          }
        })
      } else {
        $scope.headName = "添加用户";
      }
    };
    // 添加
    $scope.submit = function () {
      var msg = "添加";
      if ($scope.id) {
        msg = "编辑";
      }
      layer.load();
      $http({
        method: "put",
        url: $scope.url + "saveUser",
        data: {
          id: $scope.id,
          perName: $scope.name,
          account: $scope.account,
          citID: $scope.citID,
          sexCode: $scope.sex,
          role: $scope.role,
          email: $scope.email,
          mobile: $scope.mobile,
          useMark: $scope.useMark,
          department: $scope.dep ? {
            id: $scope.dep
          } : null
        }
      }).success(function (res) {
        layer.closeAll("loading");
        if (res.code === 0) {
          $(".user").fadeOut();
          layer.alert(msg + "成功");
          $scope.getUserList($scope.page.currentPage, $scope.page.itemsPerPage);
        } else {
          layer.alert(msg + "失败");
        }
      })
    };
    // 获取部门
    $scope.getDepList = function () {
      $http({
        method: "get",
        url: $scope.url + "getDepartmentSelect",
      }).success(function (res) {
        if (res.code === 0) {
          console.log(res);
          $scope.depList = res.data;
        }
      })
    };
    // 改变状态
    $scope.changeStatus = function (id, useMark) {
      layer.load();
      $http({
        method: "post",
        url: $scope.url + "changeUserMark",
        data: {
          id: id,
          status: useMark
        }
      }).success(function (res) {
        layer.closeAll("loading");
        if (res.code === 0) {
          $scope.getUserList($scope.page.currentPage, $scope.page.itemsPerPage)
        }
      })
    }
  }]);
  // 过滤器
  var userModule = angular.module("userFilter", []);
  // 截取字符串
  userModule.filter("subStr", function () {
    return function (str) {
      if (str && str.length > 5) {
        return str.substr(0, 5) + "...";
      } else {
        return str;
      }
    }
  });
  //审核状态
  userModule.filter("shStatus",function () {
    return function (shData) {
      if(shData===0){
        return "未审核";
      }else if (shData===1){
        return "审核通过";
      } else {
        return "审核未通过";
      }
    }
  })
})(angular);
